home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / cug232 / dictiona.st < prev    next >
Text File  |  1987-06-17  |  2KB  |  97 lines

  1. "
  2.     Dictionarys are implemented using Points in order to reduce
  3.     the number of classes in the standard prelude
  4.  
  5.     this also has the advantage of making the output appear in
  6.         key @ value
  7.     form
  8. "
  9. Class Dictionary :KeyedCollection
  10. | hashTable currentBucket currentList |
  11. [
  12.     new
  13.         hashTable <- Array new: 17
  14. |
  15.     hashNumber: aKey
  16.         ^ ( <HashNumber aKey> \\ hashTable size) + 1
  17. |
  18.     getList: aKey            | list bucketNumber |
  19.         bucketNumber <- self hashNumber: aKey.
  20.         list <- hashTable at: bucketNumber.
  21.         (list isNil)
  22.             ifTrue: [list <- List new.
  23.                  hashTable at: bucketNumber put: list].
  24.         ^ list
  25.  
  26. |
  27.     at: aKey put: anObject            | list anAssoc |
  28.  
  29.         list <- self getList: aKey.
  30.         anAssoc <- self findAssociation: aKey inList: list.
  31.         (anAssoc isNil)
  32.             ifTrue:  [anAssoc <- (Point new x: aKey) y: anObject.
  33.                   list add: anAssoc]
  34.             ifFalse: [anAssoc y: anObject].
  35.         ^ anObject
  36. |
  37.     at: aKey ifAbsent: exceptionBlock    | list anAssoc |
  38.  
  39.         list <- self getList: aKey.
  40.         anAssoc <- self findAssociation: aKey inList: list.
  41.         (anAssoc isNil)
  42.             ifTrue: [^ exceptionBlock value].
  43.         ^ anAssoc y
  44. |
  45.     removeKey: aKey ifAbsent: exceptionBlock     | list anAssoc|
  46.         
  47.         list <- self getList: aKey.
  48.         anAssoc <- self findAssociation: aKey inList: list.
  49.         (anAssoc isNil)
  50.             ifTrue: [^ exceptionBlock value].
  51.         ^ ( list remove: anAssoc
  52.              ifAbsent: [ ^ exceptionBlock value ] ) y
  53. |
  54.     findAssociation: aKey inList: linkedList
  55.  
  56.         linkedList do:
  57.             [:item | (item x = aKey) ifTrue: [^ item]].
  58.         ^ nil
  59. |
  60.     first                | item |
  61.  
  62.         (1 to: 17) do:
  63.             [:i | ((item <- self checkBucket: i) notNil)
  64.                         ifTrue: [ ^ item y] ] .
  65.         ^ nil
  66. |
  67.     next                | item |
  68.  
  69.         ((item <- currentList next) notNil)
  70.             ifTrue: [ ^ item y ].
  71.         [currentBucket < 17] whileTrue:
  72.             [currentBucket <- currentBucket + 1.
  73.              ((item <- self checkBucket: currentBucket) notNil)
  74.                 ifTrue: [ ^ item y ] ].
  75.         ^ nil
  76. |
  77.     printString
  78.         ^ (self inject: (self class printString) , ' ( '
  79.             into: [ :aString :aValue |
  80.                 aString , self currentKey printString ,
  81.                     ' @ ' , aValue printString , ' ' ]
  82.             ) , ')'
  83. |
  84.     currentKey    | clist|
  85.         ^ (currentList notNil)
  86.             ifTrue: [clist <- currentList current.
  87.                  (clist notNil) ifTrue: [clist x]
  88.                     ]
  89. |
  90.     checkBucket: bucketNumber
  91.  
  92.         ((currentList <- hashTable at:
  93.                 (currentBucket <- bucketNumber)) isNil)
  94.             ifTrue: [ ^ nil ].
  95.         ^ currentList first
  96. ]
  97.